/*
 * 文  件   名: MessageManagerImpl.java
 * 版         本: docrepository-server(Anttribe). All rights reserved
 * 描         述: <描述>
 * 修   改  人: zhaoyong
 * 修改时 间: 2014年11月24日
 */
package org.anttribe.docrepository.message.manager.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.common.errorno.SystemErrorNumber;
import org.anttribe.docrepository.message.dao.MessageDao;
import org.anttribe.docrepository.message.domain.Message;
import org.anttribe.docrepository.message.manager.MessageManager;
import org.anttribe.docrepository.user.domain.UserInfo;
import org.anttribe.saturn.common.utils.ObjectUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhaoyong
 * @version 2014年11月24日
 */
public class MessageManagerImpl implements MessageManager
{
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageManagerImpl.class);
    
    /**
     * messageDao
     */
    private MessageDao messageDao;
    
    @Override
    public Map<UserInfo, Integer> listMessageUser(String currentUser)
        throws UnifyException
    {
        if (StringUtils.isEmpty(currentUser))
        {
            LOGGER.warn("While querying, currentUser is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        Map<UserInfo, Integer> messageUserMap = new HashMap<UserInfo, Integer>();
        // 从message中查询收件人或发件人等于当前用户的Message，然后获取所有有私信来往的用户
        List<UserInfo> messageUserList = this.messageDao.queryMessageUser(currentUser);
        if (!CollectionUtils.isEmpty(messageUserList))
        {
            Map<String, Integer> senderAndMessageCountMap = new HashMap<String, Integer>();
            List<Map<String, Object>> notReadMessageCountList =
                this.messageDao.queryMessageCount(currentUser, Boolean.FALSE);
            if (!CollectionUtils.isEmpty(notReadMessageCountList))
            {
                for (Map<String, Object> notReadMessageCountMap : notReadMessageCountList)
                {
                    Object count = notReadMessageCountMap.get("count");
                    if (!ObjectUtils.isNullOrNone(count) && NumberUtils.isNumber(count.toString()))
                    {
                        senderAndMessageCountMap.put((String)notReadMessageCountMap.get("senderid"),
                            Integer.parseInt(count.toString()));
                    }
                }
            }
            for (UserInfo userInfo : messageUserList)
            {
                if (ObjectUtils.isNullOrNone(userInfo) || StringUtils.isEmpty(userInfo.getUserId()))
                {
                    continue;
                }
                
                Integer notReadMessageCount = senderAndMessageCountMap.get(userInfo.getUserId());
                if (null == notReadMessageCount)
                {
                    notReadMessageCount = 0;
                }
                messageUserMap.put(userInfo, notReadMessageCount);
            }
        }
        
        return messageUserMap;
    }
    
    @Override
    public List<Message> listReceivedMessages(String receiverId, Boolean hasRead)
        throws UnifyException
    {
        return null;
    }
    
    @Override
    public List<Message> listSendMessages(String senderId)
        throws UnifyException
    {
        return null;
    }
    
    public void setMessageDao(MessageDao messageDao)
    {
        this.messageDao = messageDao;
    }
}